//
//  CZQHomeViewController.m
//  Project_A
//
//  Created by dlios on 15-7-13.
//  Copyright (c) 2015年 zhoukaidong. All rights reserved.
//

#import "CZQHomeViewController.h"
#import "YRSideViewController.h"
#import "CZQLiftViewController.h"
#import "AppDelegate.h"
#import "BaseHeader.h"
#import "AFNetWorkHandle.h"
#import "ImageModle.h"
#import "SDCycleScrollView.h"
#import "CZQHomeTableViewCell.h"
#import "CZQHomeModel.h"
#import "XiaoKaiRaidersViewController.h"
#import "XiaoKaiAttackViewController.h"
#import "MJRefresh.h"

#define WIDTH self.view.frame.size.width / 375
#define HEIGHT self.view.frame.size.height / 667

@interface CZQHomeViewController () <UITableViewDataSource, UITableViewDelegate, SDCycleScrollViewDelegate>
@property (nonatomic, retain) UITableView *tableView;
@property (nonatomic, retain) NSMutableArray *arr;
@property (nonatomic, retain) NSMutableArray *mArr;
@property (nonatomic, retain) UIView *header;
@property (nonatomic, assign) NSInteger footer;
@end

@implementation CZQHomeViewController
- (void)dealloc
{
    [_tableView release];
    [_arr release];
    [_mArr release];
    [_header release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor whiteColor];
//    self.navigationItem.title = @"莱迪购";
//    self.navigationController.navigationBar.barTintColor = NAVCOLOR;
    self.tabBarController.tabBar.barTintColor = NAVCOLOR;
//    self.navigationController.navigationBar.translucent = NO;
    self.navigationController.navigationBarHidden = YES;
    self.arr = [NSMutableArray array];
    self.footer = 20;
    if (self.tableView != nil) {
        [self getDataWithTableViewasd];
    }
    [self getDataFromWeb];
    [self creatNav];
}

-(void)creatNav
{
    UIView *naview = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 375, 20)];
    naview.backgroundColor = NAVCOLOR;
    [self.view addSubview:naview];
    UILabel *Navlabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 20, 375 * WIDTH, 44)];
    Navlabel.backgroundColor = NAVCOLOR;
    Navlabel.text = @"主页";
    Navlabel.font = [UIFont systemFontOfSize:22];
    Navlabel.textColor = [UIColor whiteColor];
    Navlabel.textAlignment = NSTextAlignmentCenter;
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(10, 32, 20, 20);
    [button setImage:[UIImage imageNamed:@"更多"] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(soso:) forControlEvents:UIControlEventTouchUpInside];
    
    [self.view addSubview:Navlabel];
    [self.view bringSubviewToFront:Navlabel];
    [self.view addSubview:button];
    [self.view bringSubviewToFront:button];
}


- (void) getDataFromWeb
{
    self.mArr = [NSMutableArray array];
    [AFNetWorkHandle GETwithAFNByURL:@"http://api.liwushuo.com/v1/banners?channel=iOS" completion:^(id result) {
        NSArray *arr = [[result objectForKey:@"data"]objectForKey:@"banners"];
        for (NSDictionary *dic in arr) {
            CZQHomeModel *model = [[[CZQHomeModel alloc] initWithDic:dic] autorelease];
            [self.mArr addObject:model];
        }
        [self getDataWithTableView];
    }];
}

- (void) getDataWithTableView
{
    [AFNetWorkHandle GETwithAFNByURL:@"http://api.liwushuo.com/v1/channels/108/items?gender=1&generation=4&limit=20&offset=0" completion:^(id result) {
        [self.arr removeAllObjects];
        NSArray *arr = [[result objectForKey:@"data"] objectForKey:@"items"];
        for (NSDictionary *dic in arr) {
            CZQHomeModel *model = [[[CZQHomeModel alloc] initWithDic:dic] autorelease];
            [self.arr addObject:model];  
        }
        [self createTableView];
    }];
    [self.tableView reloadData];
    [self.tableView.header endRefreshing];
}

- (void)createTableView
{
    self.tableView = [[[UITableView alloc] initWithFrame:CGRectMake(0 * WIDTH, 64, 375 * WIDTH, 554 * HEIGHT) style:UITableViewStyleGrouped] autorelease];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.separatorStyle  = UITableViewCellSeparatorStyleNone;
    self.tableView.rowHeight = 200 * HEIGHT;
    [self.view addSubview:self.tableView];
    
    // tableView头部区域
    self.header = [[UIView alloc] initWithFrame:CGRectMake(0 * WIDTH, 64, 100 * WIDTH, 200 * HEIGHT)];
    self.header.backgroundColor = [UIColor redColor];
    self.tableView.tableHeaderView = self.header;
    NSMutableArray *mArr = [NSMutableArray array];
    for (CZQHomeModel *model in self.mArr) {
        [mArr addObject:model.image_url];
    }
    SDCycleScrollView *cyc = [SDCycleScrollView cycleScrollViewWithFrame:self.header.bounds imageURLStringsGroup:mArr];
    cyc.delegate = self;
    [self.header addSubview:cyc];
    if (self.arr != nil) {
        self.tableView.footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(getDataWithTableViewasd)];
    }
    self.tableView.header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(getDataWithTableView)];
}

- (void) getDataWithTableViewasd
{
    if (self.arr != nil) {
        NSString *temp = [NSString stringWithFormat:@"http://api.liwushuo.com/v1/channels/108/items?gender=1&generation=4&limit=20&offset=%ld", self.footer];
        [AFNetWorkHandle GETwithAFNByURL:temp completion:^(id result) {
            NSArray *arr = [[result objectForKey:@"data"] objectForKey:@"items"];
            for (NSDictionary *dic in arr) {
                CZQHomeModel *model = [[[CZQHomeModel alloc] initWithDic:dic] autorelease];
                [self.arr addObject:model];
            }
            if (self.tableView == nil) {
                [self createTableView];
            }
        }];
        self.footer += 20;
        [self.tableView reloadData];
        [self.tableView.footer endRefreshing];
    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.arr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CZQHomeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuse"];
    if (!cell) {
        cell = [[[CZQHomeTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"reuse"] autorelease];
    }
    CZQHomeModel *model = self.arr[indexPath.row];
    cell.model = model;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
    XiaoKaiRaidersViewController *raid = [[[XiaoKaiRaidersViewController alloc] init] autorelease];
    CZQHomeModel *model = [self.arr objectAtIndex:indexPath.row];
    raid.hidesBottomBarWhenPushed = YES;
    self.tabBarController.tabBar.hidden = YES;
    raid.webviewrequest = model.content_url;
    raid.labeltext = model.title;
    [self.navigationController pushViewController:raid animated:YES];
}

- (void)soso:(UIButton *)button

{
    AppDelegate *delegate=(AppDelegate*)[[UIApplication sharedApplication]delegate];
    YRSideViewController *sideViewController = [delegate mySideViewController];
    [sideViewController showLeftViewController:true];
    [sideViewController setRootViewMoveBlock:^(UIView *rootView, CGRect orginFrame, CGFloat xoffset) {
        rootView.frame = CGRectMake(xoffset, orginFrame.origin.y, orginFrame.size.width, orginFrame.size.height);
    }];
}

- (void)cycleScrollView:(SDCycleScrollView *)cycleScrollView didSelectItemAtIndex:(NSInteger)index
{
    XiaoKaiAttackViewController *attackViewController = [[[XiaoKaiAttackViewController alloc] init] autorelease];
    CZQHomeModel *model = [self.mArr objectAtIndex:index];
    attackViewController.myid = model.target_id;
    attackViewController.Push = 0;
    [self.navigationController pushViewController:attackViewController animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
